package com.liner.collections;

import com.liner.collection.Point;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class SortPoint {
    public static void main(String[] args) {
        ArrayList<Point> list = new ArrayList<>();
        list.add(new Point(12, 3));
        list.add(new Point(57, 51));
        list.add(new Point(31, 93));
        list.add(new Point(67, 36));
        list.add(new Point(1, 2));
        System.out.println("list原始数据:" + list);


        //jdk1.8时，List集合自身提供了sort方法进行排序，方法中需要传入比较器即可
        list.sort(new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                return o1.getY() - o2.getY();   //根据y的升序排序
            }
        });


        Collections.sort(list, new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
//                return o1.getX() - o2.getX();   //按x升序
//                return o2.getY() - o1.getY();   //按y降序
                int len1 = o1.getX() * o1.getX() + o1.getY() * o1.getY();
                int len2 = o2.getX() * o2.getX() + o2.getY() * o2.getY();
                return len1 - len2; //距离原点的长度 升序

            }
        });
        System.out.println("list自定义排序后数据:" + list);
    }
}
